package com.example.qicao.myapplication;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.orhanobut.logger.Logger;

import java.util.List;


public class RageComicListFragment extends Fragment {

  private List<TitleListModel> titleListModels;

  private OnRageComicSelected mListener;

  public static RageComicListFragment newInstance() {
    return new RageComicListFragment();
  }

  public RageComicListFragment() {
  }

  @Override
  public void onAttach(Context context) {
    super.onAttach(context);

    /*
    * 加载所有的资源
    *
    * */
    if (context instanceof OnRageComicSelected) {
      mListener = (OnRageComicSelected) context;
    } else {
      throw new ClassCastException(context.toString() + " must implement OnRageComicSelected.");
    }

    // Get rage face names and descriptions.
    final Resources resources = context.getResources();
    resources.getStringArray(R.array.images);
      String[] mNames = resources.getStringArray(R.array.names);
      String[]  mDescriptions = resources.getStringArray(R.array.descriptions);
      String[] mUrls = resources.getStringArray(R.array.urls);
      TypedArray typedArray = resources.obtainTypedArray(R.array.images);

      for(int i=0;i<mNames.length;i++){
          TitleListModel titleListModel = new TitleListModel();
          titleListModel.setTitle(mNames[i]);
          titleListModel.setImgDesc(typedArray.getDrawable(i));
      }




    // Get rage face images.
    final TypedArray typedArray = resources.obtainTypedArray(R.array.images);
    final int imageCount = mNames.length;
    typedArray.recycle();
  }

  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final SwipeRefreshLayout view = (SwipeRefreshLayout) inflater.inflate(R.layout.fragment_rage_comic_list, container, false);

    final Activity activity = getActivity();
    final RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
    recyclerView.setLayoutManager(new GridLayoutManager(activity, 2));

    final RageComicAdapter rageComicAdapter = new RageComicAdapter(activity);
    recyclerView.setAdapter(rageComicAdapter);

    view.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
      @Override
      public void onRefresh() {
          new Thread(new Runnable() {
          @Override
          public void run() {
            mNames[0] = "helloworld";
              rageComicAdapter.notifyDataSetChanged();

              Logger.i("do refresh");
              view.setRefreshing(false);
          }
        }).start();

      }
    });

    return view;
  }

  class RageComicAdapter extends RecyclerView.Adapter<ViewHolder> {

    private LayoutInflater mLayoutInflater;

    public RageComicAdapter(Context context) {
      mLayoutInflater = LayoutInflater.from(context);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
      ViewHolder viewHolder =  new ViewHolder(mLayoutInflater
          .inflate(R.layout.recycler_item_rage_comic, viewGroup, false));
      return  viewHolder;

    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {
      final int imageResId = mImageResIds[position];
      final String name = mNames[position];
      final String description = mDescriptions[position];
      final String url = mUrls[position];

      viewHolder.mNameTextView.setText(name);
      viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          mListener.onRageComicSelected(imageResId, name, description, url);
        }
      });
    }

    @Override
    public int getItemCount() {
      return mNames.length;
    }
  }

  class ViewHolder extends RecyclerView.ViewHolder {
    // Views
    private ImageView mImageView;
    private TextView mNameTextView;

    private ViewHolder(View itemView) {
      super(itemView);

      // Get references to image and name.
      mImageView = (ImageView) itemView.findViewById(R.id.comic_image);
      mNameTextView = (TextView) itemView.findViewById(R.id.name);
    }
  }

  public interface OnRageComicSelected {
    void onRageComicSelected(int imageResId, String name, String description, String url);
  }
}
